package com.kzw.oa.system.service;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.core.util.json.JSON2;
import com.kzw.misc.utils.AppUtil;
import com.kzw.misc.utils.ContextUtil;
import com.kzw.oa.system.model.AppRole;
import com.kzw.oa.system.model.AppUser;
import com.kzw.oa.system.model.Department;
import com.kzw.oa.system.model.UserOrg;

@Service
@Transactional
public class AppUserService extends DefaultEntityManager<AppUser, Long> {

	/**
	 * 获得当前用户信息
	 * */
	@Transactional(readOnly=true)
	public String getCurUserInfo(){
	    AppUser currUser = ContextUtil.getCurrentUser();
	    
	    String rolestr = ";";
	    Set<AppRole> roles = currUser.getRoles();
	    if(roles.size()>0){
	    	for (AppRole r : roles) {
	    		rolestr += r.getRoleId()+";";
			}
	    }
	    
		Department currDep = currUser.getDept();
		if (currDep == null) {// 若所属部门为空，则设置一个缺省的部门
			currDep = new Department();
			currDep.setDepId(0L);
			currDep.setDepName(AppUtil.getCompanyName());
		}
		
		Map<String, Object> map = new LinkedHashMap<>();
		map.put("userId", currUser.getUserId());
		map.put("fullname", currUser.getFullname());
		map.put("username", currUser.getUsername());
		map.put("depId", currDep.getDepId());
		map.put("depName", currDep.getDepName());
		map.put("roleIds", rolestr);
		
		String rights = currUser.getRights().toString().replace("[", "").replace("]", "");
		map.put("rights", rights);
		map.put("topModules", currUser.getTopModules().values());
		
		return new JSON2(map).toJson();
	}
	
	/**
	 * 取得组织主要负责人
	 */
	@Transactional(readOnly=true)
	public List<AppUser> getChargeOrgUsers(Department org) {
		
		Set<UserOrg> userOrgs = org.getUserOrgs();
		String isChargeUser = "";
		Iterator<UserOrg> it = userOrgs.iterator();
		while(it.hasNext()){
			UserOrg userOrg = it.next();
			if(userOrg.getIsCharge()!=null&&userOrg.getIsCharge()==1){
				isChargeUser += userOrg.getUserid()+",";
			}
		}
		isChargeUser = isChargeUser.length()==0?"0":isChargeUser.substring(0, isChargeUser.length()-1);
		String hql = "from AppUser au where au.userId in ("+isChargeUser+") and au.delFlag=0";
		return findByHQL(hql);
	}
	
	/**
	 * 按部门取得用户列表
	 */
	@Transactional(readOnly=true)
	public Page<AppUser> getDepUsers(String orgPath, PageRequest pageRequest, Map<String, String> map) {
		if(pageRequest == null){
			pageRequest = new PageRequest(9999999);
		}
		
		if(!"0.".equals(orgPath)) {
			//不显示分管领导
			String hql="select distinct uo.appUser from UserOrg uo " +
				"where uo.isPrimary=1 and uo.Department.path like ? and uo.appUser.delFlag = 0 ";
			if(map!=null){
				if(StringUtils.isNotEmpty(map.get("username").toString())){
					hql += "and uo.appUser.username like '%"+map.get("username").toString().trim()+"%' ";
				}
				if(StringUtils.isNotEmpty(map.get("fullname").toString())){
					hql += "and uo.appUser.fullname like '%"+map.get("fullname").toString().trim()+"%' ";
				}
			}
			//排序
			hql += " order by uo.appUser.orgSn asc, uo.appUser.userSn asc";
			return getEntityDao().findPage(pageRequest, hql, orgPath+"%");
		} else {
			String hql = "from AppUser au where au.delFlag=0 ";
			if(map!=null){
				if(StringUtils.isNotEmpty(map.get("username").toString())){
					hql += "and au.username like '%"+map.get("username").toString().trim()+"%' ";
				}
				if(StringUtils.isNotEmpty(map.get("fullname").toString())){
					hql += "and au.fullname like '%"+map.get("fullname").toString().trim()+"%' ";
				}
			}
			hql += " order by au.orgSn asc, au.userSn asc";
			return getEntityDao().findPage(pageRequest, hql);
		}
	}
}

